package ie.dkit.semanticPhotoWeb.server.persistence;

import java.util.Collection;
import java.util.List;

import ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Interest;
import ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Person;
import ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class FOAFDAOHibernateImpl extends HibernateTemplate implements FOAFDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#delete(ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Person)
	 */
	public void delete(Person person) {
		super.delete(person);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#findPersonById(int)
	 */
	public Person findPersonById(int id) {
		return (Person) getSession().load(Person.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#getPersons()
	 */
	@SuppressWarnings("unchecked")
	public Collection<Person> getPersons() {
		return loadAll(Person.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#save(ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Person)
	 */
	public void save(Person person) {
		saveOrUpdate(person);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#delete(ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Interest)
	 */
	public void delete(Interest interest) {
		super.delete(interest);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#findInterestById(int)
	 */
	public Interest findInterestById(int id) {
		return (Interest) getSession().load(Interest.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#getInterests()
	 */
	@SuppressWarnings("unchecked")
	public Collection<Interest> getInterests() {
		return loadAll(Interest.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#save(ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Interest)
	 */
	public void save(Interest interest) {
		saveOrUpdate(interest);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.FOAFDAO#findPersonByUsername(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public Person findPersonByUsername(String username) {
		List<Person> list = findByNamedQuery("findPersonByUsername", username);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}
}
